#pragma once

#include <iostream>
#include <string>
#include <sys/stat.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "log.hpp"
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "protocol.hpp"

class calClient
{
public:
    calClient(const std::string &serverip, const uint16_t &port)
        : _serverip(serverip), _port(port), _sock(-1)
    {
    }

    void initClient()
    {
        _sock = socket(AF_INET, SOCK_STREAM, 0);
        if (_sock < 0)
        {
            std::cerr << "socket create error" << std::endl;
            exit(2);
        }
    }

    void start()
    {
        struct sockaddr_in server;
        memset(&server, 0, sizeof(server));
        server.sin_family = AF_INET;
        server.sin_port = htons(_port);
        server.sin_addr.s_addr = inet_addr(_serverip.c_str());

        if (connect(_sock, (struct sockaddr *)&server, sizeof(server)) != 0)
        {
            std::cerr << "connect error" << std::endl;
        }
        else
        {
            std::string line;
            std::string inbuffer;
            while (1)
            {
                std::cout << "Mycal>>> ";
                std::getline(std::cin, line); // 1+1

                Request req = ParseLine(line);
                std::string content;
                req.serialize(&content);

                std::string send_string = enLength(content);

                send(_sock, send_string.c_str(), send_string.size(), 0);

                std::string package, text;
                if (!recvPackage(_sock, inbuffer, &package))
                {
                    continue;
                }
                deLength(package, &text);
                //"exitcode result"

                Response resp;
                resp.Deserialize(text);
                std::cout << "exitcode: " << resp._exitcode << std::endl;
                std::cout << "result: " << resp._result << std::endl;
            }
        }
    }

    Request ParseLine(const std::string &line)
    {
        //"1+1" "123*456" "12/0"
        int status = 0;
        int i = 0;
        int cnt = line.size();

        std::string left, right;
        char op;

        while (i < cnt)
        {
            switch (status)
            {
            case 0:
            {
                if (!isdigit(line[i]))
                {
                    op = line[i];
                    status = 1;
                }
                else
                {
                    left.push_back(line[i++]);
                }
                break;
            }
            case 1:
            {
                i++;
                status = 2;
                break;
            }
            case 2:
                right.push_back(line[i++]);
                break;
            }
        }
        return Request(std::stoi(left),std::stoi(right),op);
    }

    ~calClient()
    {
        if (_sock >= 0)
        {
            close(_sock);
        }
    }

private:
    int _sock;
    std::string _serverip;
    uint16_t _port;
};
